var gsjyys001 = {
    init:function(){
        $("#gsjyys_toolbar input[name='gsId']").jgbmtree($("#gsjyys_toolbar input[name='bmId']"),undefined,$("#gsjyys_toolbar input[name='bmId']").val(),undefined);
        $("#gsjyys_toolbar [name='nf']").numberspinner({
            min: 2000,
            max: 2100,
            editable: true
        });
        $("#gsjyys_toolbar [name='yf']").numberspinner({
            min: 1,
            max: 12,
            editable: true
        });
        gsjyys001.grid.datagrid({
            url:$.webPath()+"/cwgl/gsjyys/gsjyysList",
            singleSelect:true,
            fit:true,
            pagination:true,
            rownumbers:true,
            pageNumber: 1,
            fitColumns:true,
            toolbar:'#gsjyys_toolbar',
            columns:[
                [{field:'nf',title:'月份',width:100,formatter:function(v,r,i){
                    return v+'年'+r.yf + "月";
                },align:'center'},
                {field:'x',title:'旬',width:100,align:'center'},
                {field:'gs.jc',title:'公司',width:200,align: 'center'},
                {field:'bm.jc',title:'部门',width:200,align: 'center'},
                {field:'dateCreated',title:'制单时间',width:100,align: 'center'},
                {field:'createUser.xm',title:'制单人',width:100,align: 'center'},
                {field:'shrq',title:'审核日期',width:100,align: 'center'},
                {field:'shr.xm',title:'审核人',width:100,align: 'center'},
                {field:'zt',title:'状态',width:100,formatter:function(v,r,i){
                    return v == 'ybc'?"已保存":(v == 'ytj' ? "已提交":(v == 'ysh'?'已审核':''));
                },align: 'center'}]
            ],
            queryParams:{
                gsId:$("#gsjyys_toolbar input[name='gsId']").val()||'',
                bmId:$("#gsjyys_toolbar input[name='bmId']").val()||'',
                nf:$("#gsjyys_toolbar input[name='nf']").val()||'',
                yf:$("#gsjyys_toolbar input[name='yf']").val()||'',
                x:$("#gsjyys_toolbar input[name='x']").val()||''
            },
            onLoadSuccess:function(){
                $("#gsjyys_toolbar ul.niu a[name='ck']").hide();
                $("#gsjyys_toolbar ul.niu a[name='tj']").hide();
                $("#gsjyys_toolbar ul.niu a[name='sh']").hide();
                $("#gsjyys_toolbar ul.niu a[name='sc']").hide();
                $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
            },
            onSelect:function(rowIndex, record){
                $("#gsjyys_toolbar ul.niu a[name='ck']").show();
                if(record.zt == 'ybc'){
                    $("#gsjyys_toolbar ul.niu a[name='tj']").show();
                    $("#gsjyys_toolbar ul.niu a[name='sc']").show();
                } else {
                    $("#gsjyys_toolbar ul.niu a[name='tj']").hide();
                    $("#gsjyys_toolbar ul.niu a[name='sc']").hide();
                }
                if(record.zt == 'ytj'){
                    $("#gsjyys_toolbar ul.niu a[name='sh']").show();
                } else {
                    $("#gsjyys_toolbar ul.niu a[name='sh']").hide();
                }
            },
            onDblClickCell:function(rowIndex, field, value){
                if(field == 'nf'){
                    $("#gsjyys_toolbar ul.niu a[name='ck']:visible").click();
                }
            }
        });
    },
    grid:$("#gsjyys table[name='grid']"),
    search:function(){
        gsjyys001.grid.datagrid("load",{
            gsId:$("#gsjyys_toolbar input[name='gsId']").val()||'',
            bmId:$("#gsjyys_toolbar input[name='bmId']").val()||'',
            nf:$("#gsjyys_toolbar input[name='nf']").val()||'',
            yf:$("#gsjyys_toolbar input[name='yf']").val()||'',
            x:$("#gsjyys_toolbar input[name='x']").val()||''
        })
    },
    window:null,
    showWindow:function(type){
        var id = "";
        var t = "";
        var height = 600;
        var button = [{text:'保存',handler:function(){
            $("#gsjyys_content_grid input").removeAttr("name");
            gsjyys001.window.mask();
            $("#gsjyys_content_form").form("submit",{
                onSubmit:function(){
                    if($("#gsjyys_content_form").form("validate")){
                        return true;
                    }
                    gsjyys001.window.mask("hide");
                    return false;
                },
                success:function(data){
                    //alert(data);
                    var obj = eval("("+data+")");
                    gsjyys001.window.mask("hide");
                    //alert(obj.statusCode);
                    if(obj.statusCode == 200){
                        $.messager.show({title:'提示',msg:'填制成功！'});
                        gsjyys001.grid.datagrid("reload");
                        gsjyys001.window.window("close");
                    }else {
                        $.messager.alert("提示",obj.message,"error");
                    }
                }
            });
        }}];
//        alert(type);
        if(type == '查看'){
            var row = gsjyys001.grid.datagrid("getSelected");
            if(!gsjyys001.grid.datagrid("getSelected")){
                $.messager.alert("提示","请选择记录","info");
                return;
            }
            id = row.id;
//            alert(!!$("#gsjyys_toolbar ul.niu a[name='sh']:visible").length);
            if (row.zt == "ytj" && $("#gsjyys_toolbar ul.niu a[name='sh']:visible").length){
                button = [{text:'审核',handler:function(){
                    gsjyys001.window.mask();
                    $.post($.webPath()+"/cwgl/gsjyys/sh",{id:id},function(data){
                        gsjyys001.window.mask("hide");
                        if(data.statusCode == 200){
                            $.messager.show({title:'提示',msg:'审核成功！'});
                            gsjyys001.grid.datagrid("reload");
                            gsjyys001.window.window("close");
                        } else {
                            $.messager.alert("提示",data.message,"info");
                        }
                    });
                }}];
            } else {
                button = [];
            }
        } else if(type == '部门数据填制') {
            t = 'bmtz';
            height = 400;
        } else {
            t = 'gstz';
        }
        gsjyys001.window = $.window({
            title:type,
            width:600,
            height:height,
            cache:false,
            iconCls:type == '查看'?'icon-tip':'icon-add',
            url:$.webPath()+"/cwgl/gsjyys/gsjyys_content?id="+ id+"&type="+t,
            modal:true,
            buttons:button
        });
    }
}
$(function(){
    gsjyys001.init();
    $("#gsjyys_toolbar input:button").click(function(){
        gsjyys001.search();
    });
    $("#gsjyys_toolbar ul.niu a[name='gssjtz']").click(function(){
        gsjyys001.showWindow("公司数据填制");
    });
    $("#gsjyys_toolbar ul.niu a[name='bmsjtz']").click(function(){
        gsjyys001.showWindow("部门数据填制");
    });
    $("#gsjyys_toolbar ul.niu a[name='ck']").click(function(){
        gsjyys001.showWindow("查看");
    });
    $("#gsjyys_toolbar ul.niu a[name='tj']").click(function(){

        var row  = gsjyys001.grid.datagrid("getSelected");
        if(row){
            if(row.zt == 'ytj' || row.zt == 'ysh'){
                $.messager.alert("提示","已提交！","info");
                return;
            }
            $.messager.confirm("确定","确定提交吗？",function(v){
                if(v){
                    $.mask();
                    $.post($.webPath() + "/cwgl/gsjyys/submit",{id:row.id},function(data){
                        $.mask.hide()
                        if(data.statusCode == 200){
                            gsjyys001.grid.datagrid("reload");
                            $.messager.show({title:'提示',msg:"提交成功！"});
                        } else {
                            $.messager.alert("提示",data.message,"info");
                        }
                    });

                }
            })
        } else {
            $.messager.alert("提示","未选择行！","info");
        }
    });
    $("#gsjyys_toolbar ul.niu a[name='sh']").click(function(){
        var row  = gsjyys001.grid.datagrid("getSelected");
        if(row){
            if(row.zt == 'ysh'){
                $.messager.alert("提示","已审核！","info");
                return;
            }
            $.messager.confirm("确定","确定审核吗？",function(v){
                if(v){
                    $.mask();
                    $.post($.webPath() + "/cwgl/gsjyys/sh",{id:row.id},function(data){
                        $.mask.hide()
                        if(data.statusCode == 200){
                            gsjyys001.grid.datagrid("reload");
                            $.messager.show({title:'提示',msg:"审核成功！"});
                        } else {
                            $.messager.alert("提示",data.message,"info");
                        }
                    });
                }
            });
        } else {
            $.mask.hide()
            $.messager.alert("提示","未选择行！","info");
        }
    });
    $("#gsjyys_toolbar ul.niu a[name='sc']").click(function(){
        var row  = gsjyys001.grid.datagrid("getSelected");
        if(row){
            if(row.zt != 'ybc'){
                $.messager.alert("提示","该记录不能删除！","info");
                return;
            }
            $.messager.confirm("确定","确定删除吗？",function(v){
                if(v){
                    $.mask();
                    $.post($.webPath() + "/cwgl/gsjyys/delete",{id:row.id},function(data){
                        $.mask.hide()
                        if(data.statusCode == 200){
                            gsjyys001.grid.datagrid("reload");
//                            $.messager.show({title:'提示',msg:"审核成功！"});
                        } else {
                            $.messager.alert("提示",data.message,"info");
                        }
                    });
                }
            })
        } else {
            $.mask.hide()
            $.messager.alert("提示","未选择行！","info");
        }
    });
});